package ece496.sensorTTS;
	 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SensorTTSActivity extends Activity implements OnInitListener {

    private int MY_DATA_CHECK_CODE = 0;

    private TextToSpeech tts;

    private EditText inputText;
    private Button speakButton;
    
    public enum VoiceInstr
	{
	        Stop,
	        StopRelocalize,
	        GoStraight,
	        Rerouting,
	        TurnLeft,
	        TurnRight,
	        YouAreHere,
	        YouHaveArrived,
	        AdvanceTurnLeft,
	        AdvanceTurnRight,
	        OnYourRight,
	        OnYourLeft,
	        InYourFront,
	        YourPathInfoIs,
	        Please,
	        WalkStraightFor,
	        And,
	        YouWillArriveAtDestination,
	        MoveThruDoor,
	        M1, M2, M3, M4, M5, M6, M7, M8, M9, M10, M15, M20, M25,
	        North, South, East, West,
	        AfterTurnFacing,
	        //TurnTo,
	        //Wall,
	        //Clk10, Clk11, Clk1, Clk2, 
	        //Clk4, Clk5, Clk6, Clk7, Clk8,
	        //OnYour,
	        ObstacleIn,
	        TurnBackward,
	        FollowVoiceInstr,
	        ADDITIONAL // empty voice instructions
	    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
	
    	inputText = (EditText) findViewById(R.id.inputText);
    	speakButton = (Button) findViewById(R.id.speakButton);
	
		speakButton.setOnClickListener(new OnClickListener() {           
			public void onClick(View v) {
			    String textData = PlayAudio(VoiceInstr.OnYourLeft);
			    textData += PlayAudio(VoiceInstr.ObstacleIn);
			    textData += PlayAudio(VoiceInstr.M3);
				tts.speak(textData, TextToSpeech.QUEUE_ADD, null);
			}
      });

		Intent checkIntent = new Intent();
    	checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
    	startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
    }
    
 	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 		if (requestCode == MY_DATA_CHECK_CODE) {
 			if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
                // success, create the TTS instance
                tts = new TextToSpeech(this, this);
            }
            else {
                // missing data, install it
                Intent installIntent = new Intent();
                installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
                startActivity(installIntent);
            }
        }
    }

    public static String PlayAudio(VoiceInstr instr)
    {
    	String instr_s = null;
    	switch (instr)
    	{
    		case Stop:
    			instr_s = "Stop";
    			break;
    		case StopRelocalize:
    			instr_s = "Please stop for the device to re-localize";
    			break;
    		case GoStraight:
    			instr_s = "Go straight";
    			break;
    		case Rerouting:
    			instr_s = "Please wait for the device to re-route";
    			break;
    		case TurnLeft:
    			instr_s = "Find the way on your left";
    			break;
    		case TurnRight:
    			instr_s = "Find the way on your right";
    			break;
    		case YouAreHere:
    			instr_s = "You are here";
    			break;
    		case YouHaveArrived:
    			instr_s = "You have arrived at";
    			break;
    		case AdvanceTurnLeft:
    			instr_s = "Prepare to turn left in a few steps";
    			break;
    		case AdvanceTurnRight:
    			instr_s = "Prepare to trun right in a few steps";
    			break;
    		case OnYourRight:
    			instr_s = "On your right";
    			break;
    		case OnYourLeft:
    			instr_s = "On your left";
    			break;
    		case InYourFront:
    			instr_s = "In your front";
    			break;
    		case YourPathInfoIs:
    			instr_s = "Your path information is";
    			break;
    		case Please:
    			instr_s = "Please";
    			break;
    		case WalkStraightFor:
    			instr_s = "Walk straight for";
    			break;
    		case And:
    			instr_s = "And";
    			break;
    		case YouWillArriveAtDestination:
    			instr_s = "You will arrive at the destination";
    			break;
    		case MoveThruDoor:
    			instr_s = "Move through the door";
    			break;
    		case FollowVoiceInstr:
    			instr_s = "Please follow the voice instructions to reach";
    			break;
    		case AfterTurnFacing:
    			instr_s = "After turn, you are now facing";
    			break;
    		case TurnBackward:
    			instr_s = "Go back";
    			break;
    		case M1:
    			instr_s = "One meter";
    			break;
    		case M2:
    			instr_s = "Two meters";
    			break;    		
    		case M3:
    			instr_s = "Three meters";
    			break; 
    		case M4:
    			instr_s = "Four meters";
    			break;
    		case M5:
    			instr_s = "Five meters";
    			break;
    		case M6:
    			instr_s = "Six meters";
    			break;
    		case M7:
    			instr_s = "Seven meters";
    			break;
    		case M8:
    			instr_s = "Eight meters";
    			break;   			
    		case M9:
    			instr_s = "Nine meters";
    			break;
    		case M10:
    			instr_s = "Ten meters";
    			break;
    		case M15:
    			instr_s = "Fifteen meters";
    			break;
    		case M20:
    			instr_s = "Twenty meters";
    			break;
    		case M25:
    			instr_s = "Twenty five meters";
    			break;
    		case North:
    			instr_s = "North";
    			break;
    		case South:
    			instr_s = "South";
    			break;
    		case West:
    			instr_s = "West";
    			break;
    		case East:
    			instr_s = "East";
    			break;
    		case ObstacleIn:
    			instr_s = "Obstacle in";
    			break;
    		case ADDITIONAL:
    			instr_s = "Aditional";
    			break;   		
    	}
    	
    	return instr_s;
    }
    
    public void onInit(int status) {       
        if (status == TextToSpeech.SUCCESS) {
            Toast.makeText(SensorTTSActivity.this,
                    "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show();
        }
        else if (status == TextToSpeech.ERROR) {
            Toast.makeText(SensorTTSActivity.this,
                    "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show();
        }
    }

}